package com.zhang.server.controller;

import com.zhang.server.common.Result;
import com.zhang.server.controller.request.AdminPageRequest;
import com.zhang.server.controller.request.LoginRequest;
import com.zhang.server.controller.request.PasswordRequest;
import com.zhang.server.entity.Admin;
import com.zhang.server.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest){
        return Result.success(adminService.login(loginRequest));
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(adminService.list());
    }

    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest){
        return Result.success(adminService.page(adminPageRequest));
    }

    @PostMapping("/save")
    public Result save(@RequestBody Admin admin){
        adminService.save(admin);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(adminService.getById(id));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        adminService.update(admin);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        adminService.delete(id);
        return Result.success();
    }

    @PutMapping("/password")
    public Result updatePassword(@RequestBody PasswordRequest passwordRequest){
        adminService.updatePassword(passwordRequest);
        return Result.success();
    }

}
